import re

import requests

from main.config import path, headers


def download_m3u8(name, m3u8_url, prefix=''):
    m3u8_data = requests.get(url=m3u8_url, headers=headers).text
    m3u8_data = re.sub('#E.*', '', m3u8_data).split()

    f = open(path + name + '.mp4', mode='wb')
    for ts in m3u8_data:
        ts_url = prefix + ts
        ts_content = requests.get(url=ts_url, headers=headers).content
        f.write(ts_content)
        print('-', end='')
    f.close()
    print()

